import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'Login',
            component: () => import('@/views/user/Login.vue'),
            meta: { title: '登录' }
        },
        {
            path: '/register',
            name: 'Register',
            component: () => import('@/views/user/Register.vue'),
            meta: { title: '注册' }
        },
        {
            path: '/',
            component: Layout,
            redirect: '/dashboard',
            children: [
                {
                    path: 'dashboard',
                    name: 'Dashboard',
                    component: () => import('@/views/Dashboard.vue'),
                    meta: { title: '首页', requiresAuth: true }
                },
                {
                    path: 'question',
                    name: 'Question',
                    component: () => import('@/views/question/index.vue'),
                    meta: { title: '题库管理', requiresAuth: true}
                },
                {
                    path: 'exam',
                    name: 'Exam',
                    component: () => import('@/views/exam/ExamPaper.vue'),
                    meta: { title: '考试管理', requiresAuth: true}
                },
                {
                    path: 'record',
                    name: 'Record',
                    component: () => import('@/views/exam/ExamRecord.vue'),
                    meta: { title: '考试记录', requiresAuth: true }
                },
                {
                    path: 'wrong-questions',
                    name: 'WrongQuestions',
                    component: () => import('@/views/exam/WrongQuestions.vue'),
                    meta: { title: '错题本', requiresAuth: true }
                },
                {
                    path: 'question-QuestionList',
                    name: 'QuestionList',
                    component: () => import('@/views/question/QuestionList.vue'),
                    meta: { title: '题目管理', requiresAuth: true}
                },
                {
                    path: 'examList',
                    name: 'ExamList',
                    component: () => import('@/views/exam/ExamList.vue'),
                    meta: { title: '试卷列表', requiresAuth: true}
                },
                {
                    path: 'exam/taking/:id/:duration/:totalScore/:paperName',
                    name: 'ExamTaking',
                    component: () => import('@/views/exam/ExamTaking.vue'),
                    meta: { title: '考试', requiresAuth: true}
                },
                {
                    path: 'rule',
                    name: 'Rule',
                    component: () => import('@/views/rule/index.vue'),
                    meta: { title: '规则管理', requiresAuth: true}
                },
                {
                    path: 'profile',
                    name: 'Profile',
                    component: () => import('@/views/user/Profile.vue'),
                    meta: { title: '个人中心', requiresAuth: true }
                },
            ]
        },
        {
            path: '/training',
            component: Layout,
            redirect: '/training/list',
            name: 'Training',
            meta: { title: '训练管理', icon: 'Document' },
            children: [
                {
                    path: 'list',
                    name: 'TrainingList',
                    component: () => import('@/views/training/TrainingList.vue'),
                    meta: { title: '训练列表', icon: 'List' }
                },
                {
                    path: 'paper/:id',
                    name: 'TrainingPaper',
                    component: () => import('@/views/training/TrainingPaper.vue'),
                    meta: { title: '训练试卷', icon: 'Document', activeMenu: '/training/list' },
                    hidden: true
                },
                {
                    path: 'detail/:id',
                    name: 'TrainingDetail',
                    component: () => import('@/views/training/TrainingDetail.vue'),
                    meta: { title: '训练详情', icon: 'Document', activeMenu: '/training/list' },
                    hidden: true
                }
            ]
        },
        {
            path: '/profile',
            component: Layout,
            hidden: true,
            children: [
                {
                    path: '',
                    name: 'Profile',
                    component: () => import('@/views/profile/index.vue'),
                    meta: { title: '个人中心', icon: 'User' }
                }
            ]
        }
    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    const userRole = localStorage.getItem('userRole')

    // 设置页面标题
    document.title = to.meta.title ? `${to.meta.title} - 在线考试系统` : '在线考试系统'

    // 检查是否需要认证
    if (to.meta.requiresAuth && !token) {
        next('/login')
        return
    }

    // 检查角色权限
    if (to.meta.role && to.meta.role !== userRole) {
        next('/dashboard')
        return
    }

    next()
})

export default router 